/**
 * Created by WT-SUN on 2017/4/14.
 */
$(function () {
    $('#stationTb').datagrid({
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        pagination: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/occupationalHealth/station/stationTable.shtml',
        queryParams:{
            sort:'addTime',
            order:'desc'
        },
        columns: [[
            {
                field: 'id',
                checkbox: true
            },
            {field: 'monitorTypeName', title: '监测类别', width: 100},
            {field: 'monitorProject', title: '监测项目', width: 100,formatter: function (value, row, index) {
                return "<a href='javascript:void(0)' class='easyui-linkbutton text-primary' plain='true'   onclick='infoMonData(\""+row.id+"\");'>"+row.monitorProject+"</a>";
            }},
            {field: 'monitorValue', title: '监测值', width: 100, sortable: true},
            {field: 'monitorDate', title: '监测日期', width: 100, sortable: true},
            {field: 'years', title: '年度', width: 100},
            {field: 'companyName', title: '单位名称', width: 100},
            {field: 'positionIdName', title: '岗位', width: 100, sortable: true},
            {field: 'isOverproof', title: '是否超标', width: 100,formatter: function (value, row, index) {
                return row.isOverproof == '1' ? '<span class="color-success text-white" style="padding: 3px;border-radius:5px;">否</span>' : '<span class="color-important text-white" style="padding: 3px;border-radius:5px;">是</span>';
            }},
            {field: 'testDate', title: '监测周期(年/次)', width: 150},
            {field: 'mac', title: 'MAC', width: 100},
            {field: 'pctwa', title: 'PC-TWA', width: 100},
            {field: 'pcstel', title: 'PC-STEL', width: 100},
            {field: 'userName', title: '登记人', width: 100},
            {field: 'deptName', title: '登记部门', width: 100},
            {field: 'addTime', title: '登记时间', width: 100}
        ]]
    });

    $('#btn_station_add').on('click',function () {
        addStation();
    });
    $('#btn_station_delete').on('click',function () {
        deleteStation();
    });
    $('#btn_station_edit').on('click',function () {
        editStation();
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm').form('reset');
    });
    queryYearthList("years");
    var isMonitorData=[{id:'',text:'请选择'},{id:'0',text:'是'},{id:'1',text:'否'}];
    $("#isOverproof").combobox({
        data: isMonitorData,
        prompt:'请选择',
        valueField: 'id',
        textField: 'text',
        editable: false,
        panelHeight: '150',
        selectOnNavigation:true
    });
    /**
     * 高级查询
     */
    $('#btn_advanced_search').on("click", function () {
        $('#queryForm2').toggle();
        $('#stationTb').datagrid('resize');
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm1').form('reset');
        $('#queryForm2').form('reset');
    });
    queryDepartmentTree();
    /**
     * 绑定监测单位选择事件
     */
    $("#monitorCompanyId").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                openSerViceTb();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');
                $("#monitorCompanyId").val('');
            }
        }]
    });
    /**
     * 绑定人员选择事件
     */
    $("#searchAddUser").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_userId_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');

            }
        }]
    });
});
var add_userId_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){
            getSelectedUser(data);
        }
    });
};
/**
 * 人员信息选择
 */
var getSelectedUser = function (data) {
    $("#searchAddUser").textbox('setValue', data[0].id).textbox('setText', data[0].realName);
};
function openSerViceTb() {
    WindowManager.openLayer({
        type: 2,
        title: '选择服务机构',
        area: ['850px', '70%'],
        content: ROOT_PATH + '/occupationalHealth/hseServiceOrg/orgTb.shtml',
        callBack:function(data){
            getSelectedOrg(data);
        }
    });
}
function getSelectedOrg(data) {
    $("#monitorCompanyId").textbox('setValue', data[0].id).textbox('setText', data[0].serviceName);
}
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            deptCom('searchAddDept',result);
        }
    });
}
function deptCom(id,result) {
    var deptId='#'+id;
    $(deptId).combotree(
        {
            data:result,
            require:true,
            prompt:'请选择',
            loadFilter:function(data){
                var newData = new Array();
                newData.push({id:"",text:'请选择'});
                newData.push(data[0]);
                return newData;
            }
        }
    );
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#stationTb').datagrid('clearSelections');
    var queryParams1 = $('#queryForm1').serializeJson();
    var queryParams2 = $('#queryForm2').serializeJson();
    var queryParams = $.extend(queryParams1,queryParams2);
    queryParams["sort"] = 'addTime';
    queryParams["order"] = 'desc';
    $('#stationTb').datagrid('options').queryParams = queryParams;
    $("#stationTb").datagrid('reload', ROOT_PATH + '/occupationalHealth/station/stationTable.shtml');
}
/**
 * 打开弹框
 * @param title
 * @param content
 */
function openLayers(title, content) {
    parent.layer.open({
        type: 2,
        resize: false,
        title: title,
        area: ['1150px', '80%'],
        content: content
    });
}
/**
 * 打开新建
 */
function addStation() {
    var title = '新增岗位值';
    var content = ROOT_PATH + '/occupationalHealth/station/add.shtml?resourceId=' + $.getQueryParam('resourceId');

    parent.layer.open({
        type: 2,
        resize: false,
        title: title,
        area: ['1150px', '60%'],
        content: content
    });
}
/**
 * 删除
 */
function deleteStation() {
    var rows = $('#stationTb').datagrid('getSelections');

    if (rows.length == 0) {
        layer.alert("选择需要删除的行！");
        return
    }
    var id = rows[0].id;
    layer.confirm('是否确定删除当前选中数据？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            type: 'post',
            url: ROOT_PATH + '/occupationalHealth/station/deleteById.shtml',
            data: {id: id},
            dataType: "json",
            success: function (result) {
                //result = $.parseJSON(result);
                if (result.success) {
                    $('#stationTb').datagrid('clearSelections');
                    reLoadData();
                    layer.msg('删除成功', {
                        icon: 1,
                        time: 1000
                    });
                }
                else {
                    layer.msg('删除失败', {
                        icon: 2,
                        time: 1000
                    });
                }
            }
        });
    });
}
/**
 * 编辑
 */
function editStation() {
    var rows = $('#stationTb').datagrid('getSelections');
    if (rows.length == 0) {
        layer.alert("选择需要编辑的行！");
        return
    }
    var id = rows[0].id;
    var title = '编辑岗位值';
    var content = ROOT_PATH + '/occupationalHealth/station/info.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id=' + id+'&type=edit';
    openLayers(title, content);
}
function infoMonData(id) {
    var title = '岗位值详情';
    var content = ROOT_PATH + '/occupationalHealth/station/info.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id=' + id+'&type=info';
    openLayers(title, content);
}
function queryYearthList(id){
    var date = new Date();
    var year = date.getFullYear();
    var yearthData = new Array();
    yearthData.push({id:"",text:"请选择"});
    for(var i =0;i<6;i++){
        var y = {id:year-i,text:(year-i)+'年'};
        yearthData.push(y);
    }
    $('#'+id).combobox(
        {
            data:yearthData,
            valueField:'id',
            textField:'text'
        }
    );
}

